package org.component.container 
{
	import org.component.interfaces.IFlipPageItem;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author Terry
	 */
	public class FlipPage extends EventDispatcher 
	{
		
		private var _assest:MovieClip = null;//ui
		
		private var _itemClass:Class = null;//子类
		
		private var _colmu:int = 1;//行;
		
		private var _row:int = 1;//列;
		
		private var _attributes:Object = null;//属性;
		
		private var _items:Array = null;
		
		private var _data:Array = null;//数据;
		
		private var _total:int = 1;//总页数;
		
		private var _btn_next:SimpleButton = null;//下一页
		
		private var _btn_prev:SimpleButton = null;//上一页
		
		private var _txt_count:TextField = null;//文本;
		
		private var _count:int = 1;
		
		/**
		 * 
		 * @param	ui  userinterface
		 * @param	itemClass 
		 * @param	colmu
		 * @param	row
		 */
		public function FlipPage(ui:MovieClip = null,itemClass:Class = null,colmu:int = 1,row:int = 1,attributes:Object = null) 
		{
			_assest = ui;
			_itemClass = itemClass;
			_colmu = colmu;
			_row = row;	
			_attributes = attributes;
			initialization();
			configureEventListeners();
		}
		
		/**
		 * 初始化UI；
		 */
		private function initialization():void
		{
			
			_items = [];
			_count = _colmu * _row;
			
			var index:int = 0;
			var item:IFlipPageItem = null;
			while (index < _count)
			{
				item = new _itemClass(_assest[_attributes["name"] + index]);
				item.setSubject(this);
				_items[_items.length] = item;
				index++;
			}
			item = null;
			
			_btn_next = _assest[_attributes["next"]]||new SimpleButton();
			_btn_prev = _assest[_attributes["prev"]]||new SimpleButton();
			_txt_count = _assest[_attributes["txt"]]||new TextField();
		}
		/**
		 * 添加事件;
		 */
		private function configureEventListeners():void
		{
			_btn_next.addEventListener(MouseEvent.CLICK, onNextClick);
			_btn_prev.addEventListener(MouseEvent.CLICK, onPrevClick);
			
		}
		private function onNextClick(event:MouseEvent):void
		{
			current++;
		}
		
		private function onPrevClick(event:MouseEvent):void
		{
			current--;
		}
		
		
		/**
		 * 刷新数据;
		 * @param	data
		 */
		public function update(data:Array = null):void
		{
			_data = data||[];
			_total = Math.ceil(_data.length / _items.length);
			
			if (_total <= 0) 
			{
				current = 0;
			}else
			{
				current = 1;
			}
			
		}
		
		private function scrollToIndex(index:int = 0):void
		{
			var data:Array = _data.slice((index-1) * _count, index * _count);
			var index:int = 0;
			
			while (index < _count)
			{
				if (data[index])
				{
					IFlipPageItem(_items[index]).update(data[index]);
				}else
				{
					IFlipPageItem(_items[index]).dispose();
				}
				
				index++;
			}
		}
		private var _current:int = 0;
		public function get current():int { return _current; }
		public function set current(value:int):void 
		{
			_current = value;
			
			if (value == 1)
			{
				_btn_prev.mouseEnabled = false;
				_btn_next.mouseEnabled = true;
			}
			else if (value >= _total)
			{
				_btn_next.mouseEnabled = false;
				_btn_prev.mouseEnabled = true;
			}else
			{
				_btn_next.mouseEnabled = true;
				_btn_prev.mouseEnabled = true;
			}
			if (_total < 2)
			{
				_btn_next.mouseEnabled = false;
				_btn_prev.mouseEnabled = false;
			}

			_txt_count.text = value+"/"+_total;
			scrollToIndex(value);
		}
		
		
	}

}